//
//  DSZTJViewController.m
//  7.13苏宁框架
//
//  Created by lovena on 15/7/14.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZTJViewController.h"
#import "DSZMainViewController.h"
#import "DSZTJViewController.h"
#import "DSZTJCollectionViewCell.h"
#import "DSZTJSuperModel.h"
#import "MJExtension.h"
#import "DSZTJGroupModel.h"
#import "DSZTJShowModel.h"
#import "DSZTJCommentModel.h"
#import "WaterflowLayout.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "DSZTJXQViewController.h"
#import "DSZShowViewController.h"
#import "UMSocial.h"
#import "DSZGZXQViewController.h"
    NSInteger groupNum;
@interface DSZTJViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,DSZTJCollectionViewCellDElegate,UITextViewDelegate>
{
    NSInteger groupNumOne;
    UICollectionView *_collection;
    UITextView *text;
}
@property(nonatomic,retain)NSArray *superModel;

@end

@implementation DSZTJViewController
NSString * const ID= @"DSZTJ";
- (void)viewDidLoad {
    [super viewDidLoad];
//    text=[[UITextView alloc]initWithFrame:CGRectMake(0, 200, 320, 480)];
//    [self.view addSubview:text];
//    text.delegate=self;
//    [text becomeFirstResponder];
    [self superModel];
    _collection.frame=CGRectMake(0, 0, 320, 480);
    WaterflowLayout *flow=[[WaterflowLayout alloc]init];
    [flow attrsArray:_groupModel];

    _collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, -7, 320, 377) collectionViewLayout:flow];
    _collection.backgroundColor=[UIColor whiteColor];
    _collection.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_collection];
    //[self.view insertSubview:_collection atIndex:1];
    [self.view sendSubviewToBack:_collection];
    _collection.delegate=self;
    _collection.dataSource=self;
    _collection.backgroundColor=[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    // 3.注册
    UINib *nib = [UINib nibWithNibName:[DSZTJCollectionViewCell xib] bundle:nil];
    [_collection registerNib:nib forCellWithReuseIdentifier:ID];
//    [_collection addFooterWithTarget:self action:@selector(loadMoreShops)];
//    [_collection addHeaderWithTarget:self action:@selector(loadShops)];
}

-(void)viewWillAppear:(BOOL)animated{
    DSZShowViewController *show=(DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:NO];
}

//-(void)loadShops{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [_collection reloadData];
//        [_collection headerEndRefreshing];
//    });
//}
//- (void)loadMoreShops
//{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        //NSArray *shopArray = [ShopModel objectArrayWithFilename:@"1.plist"];
//        
//        //[self.shops addObjectsFromArray:shopArray];
//        [_collection reloadData];
//        [_collection footerEndRefreshing];
//    });
//}

//plist文件的数组
-(NSArray*)superModel{
    //plist文件的数组
    NSArray *array=[NSArray array];
    array=[DSZTJSuperModel modelArrayWithFilename:@"Show.plist"];
    
    //group组的数组
     NSMutableArray *arraymodel=[NSMutableArray array];
    for (DSZTJSuperModel *model in array) {
        NSDictionary *dic=[model keyValues];
        NSArray *groupArray=dic[@"group"];
        groupNumOne=groupNumOne+groupArray.count;
        
        NSArray *groupmodel=[DSZTJGroupModel modelArrayWithDictArray:groupArray];
        
        for (DSZTJGroupModel *model in groupmodel) {
           // NSLog(@"%@",model);
            [arraymodel addObject:model];
        }
        
    }
    //调用方法获取group数组
    [self groupModel:arraymodel];
    
    return _superModel=array;
}

//返回group数组
-(NSArray*)groupModel:(NSArray*)arraymodel{
    return _groupModel=arraymodel;
}

-(void)TJXQview{
    DSZTJXQViewController *view=[[DSZTJXQViewController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
}
-(void)GZXQview{
    DSZGZXQViewController *view=[[DSZGZXQViewController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
}

-(void)comment{
//    UITextView *text=[[UITextView alloc]init];
//    [self.view addSubview:text];
    [text becomeFirstResponder];
}

-(void)share{
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"559f929067e58e3ddf00500d"
                                          shareText:@"唯品会天天特卖，全球高品质奶粉，正品保证"
                                         shareImage:[UIImage imageNamed:@"icon.png"]
                                    shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToDouban,UMShareToQzone,UMShareToEmail]
                                           delegate:nil];
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
    return groupNumOne;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DSZTJCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.delegate=self;
    DSZTJGroupModel *model= _groupModel[indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]]];
    
    NSString *str=model.name;
    CGFloat i=str.length;
    CGRect len=cell.nameLabel.frame;
    CGRect len1=cell.vipimage.frame;
    len=CGRectMake(cell.nameLabel.frame.origin.x, cell.nameLabel.frame.origin.y, 10+i*15, cell.nameLabel.frame.size.height);
    len1=CGRectMake(cell.nameLabel.frame.origin.x+10+i*15, cell.vipimage.frame.origin.y,cell.vipimage.frame.size.width, cell.vipimage.frame.size.height);
    cell.vipimage.frame=len1;
    cell.nameLabel.frame=len;

    cell.nameLabel.text=model.name;
    cell.intorLabel.text=model.intor;
    
    NSDictionary *dic =[model keyValues];
    NSArray *array=dic[@"show"];
    NSArray *showarray=[DSZTJShowModel modelArrayWithDictArray:array];

    
    DSZTJShowModel *showmodel=showarray[0];
    [cell.contentImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",showmodel.image]]];
    cell.contentTitleLabel.text=showmodel.title;
    [cell modelTable:showmodel group:model];

    cell.backgroundColor=[UIColor whiteColor];
    return cell;
}

//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    DSZTJGroupModel *model= _groupModel[indexPath.row];
//     NSDictionary *dic =[model keyValues];
//    NSArray *array=dic[@"show"];
//    NSArray *showarray=[DSZTJShowModel modelArrayWithDictArray:array];
//    DSZTJShowModel *showmodel=showarray[0];
//    DSZTJCollectionViewCell *cell=[[DSZTJCollectionViewCell alloc]init];
//    [cell modelTable:showmodel group:model];
//
//}
//返回首页
- (IBAction)returnMain:(UIBarButtonItem *)sender {
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DSZMainViewController *vc=[story instantiateViewControllerWithIdentifier:@"main"];
    [self presentViewController:vc animated:NO completion:nil];
}
@end
